//
//  FKPosterPopView.m
//  WanTao
//
//  Created by Macbook Pro on 2019/4/4.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKPosterPopView.h"

@interface FKPosterPopView ()

@property (nonatomic, weak) UIView *container;
@property (nonatomic, weak) UIImageView *imageView;

@property (nonatomic, weak) UIImageView *qrcodeView;
@property (nonatomic, weak) UILabel *nameLabel;

@end


@interface FKPosterData ()

@end

@implementation FKPosterPopView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews
{
    self.backgroundColor = [UIColor clearColor];
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(close)]];
    
    CGFloat containerW = fkScreenW - 40;
    CGFloat containerH = fkScreenH  - 120;
    UIView *container = [[UIView alloc] initWithFrame:CGRectMake(20, 60, containerW, containerH)];
    [self addSubview:container];
    self.container = container;
    container.backgroundColor = [UIColor whiteColor];
    [container fk_viewCornerRadius:10 borderWidth:0 borderColor:nil];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    [container addSubview:imageView];
    self.imageView = imageView;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    
    UIView *bottomView = [[UIView alloc] init];
    [container addSubview:bottomView];
    
    UIImageView *qrcodeView = [[UIImageView alloc] init];
    [bottomView addSubview:qrcodeView];
    self.qrcodeView = qrcodeView;
    
    UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor666666 text:@"顽淘亲子，共度欢乐时光"];
    [bottomView addSubview:titleLabel];
    
    NSString *name = [FKCacheTool userInfo][@"nickname"];
    UILabel *nameLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333 text:name];
    [bottomView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    UIImageView *tipView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"easyBuy"]];
    [bottomView addSubview:tipView];
    
    UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
    [bottomView addSubview:iconView];
    
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.height.offset(fk_adjustW(100));
    }];
    [qrcodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(fk_adjustW(75));
        make.left.offset(8);
        make.centerY.offset(0);
    }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(qrcodeView);
        make.left.equalTo(qrcodeView.mas_right).offset(10);
    }];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel);
        make.centerY.equalTo(qrcodeView);
    }];
    [tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel);
        make.bottom.equalTo(qrcodeView);
        make.width.offset(fk_adjustW(130));
        make.height.offset(fk_adjustW(13.5));
    }];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(fk_adjustW(60));
        make.centerY.equalTo(qrcodeView);
        make.right.offset(-8);
    }];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.bottom.equalTo(bottomView.mas_top);
    }];
//    imageView.backgroundColor = [UIColor redColor];
}

- (void)showWithImagae:(UIImage *)image
{
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:self];
    self.frame = window.bounds;
    
    self.imageView.image = image;
    self.backgroundColor = fkColorAlpha(0, 0, 0, 0.5);
}

- (void)showWithUrl:(NSString *)url
{
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:self];
    self.frame = window.bounds;
    
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:url]];
    self.backgroundColor = fkColorAlpha(0, 0, 0, 0.5);
}

- (void)showWith:(FKPosterData *)data
{
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:self];
    self.frame = window.bounds;
    
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:data.picUrl]];
    self.qrcodeView.image = [UIImage fk_imageGenerateQRCode:data.qrcodeUrl width:120 height:120];
    self.backgroundColor = fkColorAlpha(0, 0, 0, 0.5);
}

- (void)close
{
    self.container.frame = CGRectMake(fkScreenW * 0.5, fkScreenH * 0.5, 0, 0);
    [self removeFromSuperview];
}

@end

@implementation FKPosterData


@end
